//
//  WBImagesContainView.m
//  UILbrary
//
//  Created by Apple on 2023/3/2.
//  Copyright © 2023 mac. All rights reserved.
//

#import "WBImagesContainView.h"
@import PhotosUI;



@implementation WBSelectResultImgView

//MARK: - system

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self p_configSubviews];
        [self test];
    }
    return self;
}

//MARK: - test

- (void)test
{
    if (@available(iOS 15.0, *)) {
        self.deleteButton.backgroundColor = [UIColor systemMintColor];
    } else {
        // Fallback on earlier versions
    }
}

//MARK: - public

- (void)loadImgId:(NSString *)imgId
{
    self.imgId = imgId;
    PHFetchResult<PHAsset *> * result = [PHAsset fetchAssetsWithLocalIdentifiers:@[imgId] options:nil];
    PHAsset *asset = result.firstObject;
    
    if(!asset) return;

    [[PHImageManager defaultManager] requestImageForAsset:asset targetSize:CGSizeMake(60, 60) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
        dispatch_async(dispatch_get_main_queue(), ^{
            self.imgView.image = result;
        });
    }];
}

//MARK: - private

- (void)p_configSubviews
{
    [self addSubview:self.imgView];
    [self addSubview:self.deleteButton];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self.deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(self);
        make.width.mas_equalTo(20);
        make.height.mas_equalTo(20);
    }];
}

//MARK: - action

- (void)deleteButtonAction
{
    if(!self.deleteBlock) return;
    self.deleteBlock();
}

//MARK: - get

- (UIImageView *)imgView
{
    if(!_imgView){
        _imgView = [UIImageView new];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.clipsToBounds = YES;
        _imgView.layer.cornerRadius = 2.0;
    }
    return _imgView;
}

- (UIButton *)deleteButton
{
    if(!_deleteButton){
        _deleteButton = [UIButton new];
        [_deleteButton addTarget:self action:@selector(deleteButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteButton;
}

@end


@interface WBAddImgView()

@end

@implementation WBAddImgView

//MARK: - system

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self p_configSubviews];
//        [self test];
    }
    return self;
}

- (void)drawRect:(CGRect)rect
{
    [super drawRect:rect];
    [self p_drawDashedBorder];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    [self.addButton layoutButtonWitdEdgeInsetsStyle:HSFButtonEdgeInsetsStyleTop imageTitleSpace:10];
}
//MARK: - test

- (void)test
{
    self.addButton.backgroundColor = [UIColor systemRedColor];
}

//MARK: - private

- (void)p_configSubviews
{
    [self addSubview:self.addButton];
    [self.addButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
}

// add Dashed border
- (void)p_drawDashedBorder
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    CGRect shapeRect = self.bounds;
    [shapeLayer setBounds:shapeRect];
    [shapeLayer setPosition:CGPointMake(CGRectGetMidX(shapeRect), CGRectGetMidY(shapeRect))];
    [shapeLayer setFillColor:[[UIColor clearColor] CGColor]];
    [shapeLayer setStrokeColor:[[UIColor JX_GrayColor_9] CGColor]];
    [shapeLayer setLineWidth:1.0f];
    [shapeLayer setLineJoin:kCALineJoinRound];
    [shapeLayer setLineDashPattern:@[@6, @2]];
    UIBezierPath *shapepath = [UIBezierPath bezierPathWithRoundedRect:CGRectInset(shapeRect, 0, 0) cornerRadius:2];
    [shapeLayer setPath:shapepath.CGPath];
    [self.layer addSublayer:shapeLayer];
}

//MARK: - action

- (void)addButtonAction
{
    if(!self.addBlock) return;
    self.addBlock();
}

//MARK: - get

- (UIButton *)addButton
{
    if(!_addButton){
        _addButton = [UIButton new];
        [_addButton setImage:[UIImage imageNamed:@"icon_uploadImg"] forState:UIControlStateNormal];
        [_addButton setTitle:@"上传图片" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor JX_GrayColor_5] forState:UIControlStateNormal];
        _addButton.titleLabel.font = [UIFont systemFontOfSize:11];
        [_addButton addTarget:self action:@selector(addButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}


@end

@interface WBImagesContainView()


@end


@interface WBImagesContainView(test)

- (void)test;

@end

@implementation WBImagesContainView

//MARK: - system

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self){
        [self p_initConfig];
//        [self test];
    }
    return self;
}

//MARK: - public

- (void)addImgList:(NSArray <UIView *>*)views
{
    [self p_removeAllSubviews];
    
    if(!views || views.count <= 0) return;
    
    [views enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self addSubview:obj];
    }];
    
    
    NSInteger warpCount = 5;
    CGFloat fixedItemSize = 60;
    CGFloat marge = Space_big;
    CGFloat fixedInteritemSpacing = (SCREEN_WIDTH - warpCount * fixedItemSize ) / (warpCount - 1);
    CGFloat fixedLineSpacing = 10;
    
    [self.subviews mas_distributeSudokuViewsWithFixedItemWidth:fixedItemSize
                                               fixedItemHeight:fixedItemSize
                                              fixedLineSpacing:fixedLineSpacing
                                         fixedInteritemSpacing:fixedInteritemSpacing
                                                     warpCount:warpCount
                                                    topSpacing:marge
                                                 bottomSpacing:marge
                                                   leadSpacing:marge
                                                   tailSpacing:marge];
}

//MARK: -  private

- (void)p_initConfig
{
    
}

- (void)p_removeAllSubviews
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
}

@end

@implementation WBImagesContainView(test)

- (void)test
{
    self.backgroundColor = [UIColor systemRedColor];
    
    NSMutableArray *imgList = [@[] mutableCopy];
    for (NSInteger i=0; i<10; i++) {
        WBSelectResultImgView *view = [WBSelectResultImgView new];
        view.deleteBlock = ^{
            
        };
        view.backgroundColor = [UIColor randomColor];
        [imgList addObject:view];
    }
    [self addImgList:[imgList copy]];
}


@end
